package com.baizhi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.baizhi.dto.LoginFormDTO;
import com.baizhi.dto.Result;
import com.baizhi.dto.UserDTO;
import com.baizhi.utils.RedisConstants;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baizhi.entity.User;
import com.baizhi.mapper.UserMapper;
import com.baizhi.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gange
 * @since 2023-04-06
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public Result gernateCode(String phone) {
        //1.生成一个验证码字符串
        String code = RandomUtil.randomNumbers(6);
        //2.把验证码放入到redis里面
        redisTemplate.opsForValue().set(RedisConstants.LOCK_SHOP_KEY+phone,code,1, TimeUnit.MINUTES);
        //3.给手机发短信,把验证码打印到控制台
        log.info("给手机发送了验证码"+code);
        return Result.ok();
    }

    @Override
    public Result loginByCode(LoginFormDTO user, String sessionId) {
       /* //1.对比redis中的验证码,和用户的验证码是否一致
        String redisCode = (String) redisTemplate.opsForValue().get(RedisConstants.LOGIN_USER_KEY + user.getPhone());
        if(user.getCode().equals(redisCode)){
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("phone",user.getPhone());
            User userDb = baseMapper.selectOne(wrapper);
            if(ObjectUtil.isEmpty(userDb)){
                //注册
                userDb = new User();
                userDb.setCreateTime(LocalDateTime.now());
                userDb.setIcon("");
                userDb.setNickName(user.getPhone());
                userDb.setPhone(user.getPhone());
                userDb.setUpdateTime(LocalDateTime.now());
                baseMapper.insert(userDb);
            }
            //3.把登录信息放入到redis里面
            UserDTO userDTO = BeanUtil.copyProperties(userDb,UserDTO.class);
            redisTemplate.opsForValue().set(RedisConstants.LOGIN_USER_KEY+sessionId,userDTO);
            return Result.ok(sessionId);
        }
        return Result.fail("验证码无效");*/
        //        1.对比redis中的验证码，和用户的验证码是否一致。
        String redisCode = (String)redisTemplate.opsForValue().get(RedisConstants.LOGIN_CODE_KEY+user.getPhone());
        if(user.getCode().equals(redisCode)){
            //        2.判断数据库表里面是否有该用户信息，没有就创建一个用户信息，保存。
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("phone",user.getPhone());
            User userDb = baseMapper.selectOne(wrapper);
            if(ObjectUtil.isEmpty(userDb)){
                //注册
                userDb = new User();
                userDb.setCreateTime(LocalDateTime.now());
//                userDb.setIcon("/imgs/blogs/blog1.jpg");
                userDb.setNickName(user.getPhone());
                userDb.setPhone(user.getPhone());
                userDb.setUpdateTime(LocalDateTime.now());
                baseMapper.insert(userDb);
            }
            //        3.把登录信息放入到redis里面。
//            UserDTO userDTO = BeanUtil.copyProperties(userDb, UserDTO.class);
//            redisTemplate.opsForValue().set(RedisConstants.LOGIN_USER_KEY+sessionId,userDTO);
//            //        4.响应登录成功。
//            return Result.ok(sessionId);
//        }
//
//        //登录失败
//        return Result.fail("验证码无效！！！");
            UserDTO userDTO = new UserDTO();
            BeanUtil.copyProperties(userDb,userDTO);
            redisTemplate.opsForValue().set(RedisConstants.LOGIN_USER_KEY + sessionId, userDTO);

        }

        return Result.ok(sessionId);
    }

    @Override
    public Result loginByPwd(LoginFormDTO loginFormDTO, String id) {
        return null;
    }
}
